package com.kakarote.crm9.erp.jxc.service;


import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.kakarote.crm9.erp.jxc.entity.GoodsSuppliers;
import com.kakarote.crm9.utils.BaseUtil;
import com.kakarote.crm9.utils.PageData;
import com.kakarote.crm9.utils.R;

import java.util.Date;
import java.util.List;

public class GoodsSuppliersService {
    /**
     * 查询供应商列表
     * @param pd
     * @return
     */
    public List<Record> list(PageData pd){
        int pageNum = pd.get("pageNum",Integer.class);
        int pageSize = pd.get("pageSize",Integer.class);
        pageNum = (pageNum-1)*pageSize;

        StringBuilder builder =new StringBuilder();
        builder.append(Db.getSql("jxc.GoodsSuppliers.queryList"));
        if(pd.get("suppliersName")!=null) builder.append(" WHERE suppliers_name like concat('%',?, '%')  ");
        builder.append(" LIMIT ?, ?");

        if(pd.get("suppliersName")!=null){
            return Db.find(builder.toString(),pd.get("suppliersName"),pageNum,pageSize);
        }else{
            return Db.find(builder.toString(),pageNum,pageSize);
        }
    }

    /**
     * 根据供应商id查询
     * @param id
     * @return
     */
    public Record getInfo(Integer id){

        return Db.findFirst(Db.getSql("jxc.GoodsSuppliers.queryId"),id);
    }

    /**
     * 新增供应商
     * @param pd
     * @return
     */
    public R add(PageData pd){
        GoodsSuppliers suppliers = new GoodsSuppliers();
        suppliers.set("suppliers_name",pd.get("suppliersName"))
                .set("create_by",BaseUtil.getUser().getUsername())
                .set("create_time",new Date());

        if(pd.get("username")!=null) suppliers.set("username",pd.get("username"));
        if(pd.get("phone")!=null)suppliers.set("phone",pd.get("phone"));
        if(pd.get("addr")!=null)suppliers.set("addr",pd.get("addr"));
        if(pd.get("email")!=null)suppliers.set("email",pd.get("email"));

        return suppliers.save()?R.ok():R.error("添加失败");
    }

    /**
     * 修改供应商信息
     * @param pd
     * @return
     */
    public R edit(PageData pd){
        if(pd.get("id")!=null||!"".equals(pd.get("id"))){
            GoodsSuppliers suppliers = new GoodsSuppliers();
            suppliers.set("suppliers_name",BaseUtil.getUser().getUsername())
                    .set("id",pd.get("id"));
            if(pd.get("username")!=null)suppliers.set("username",pd.get("username"));
            if(pd.get("phone")!=null)suppliers.set("phone",pd.get("phone"));
            if(pd.get("addr")!=null) suppliers.set("addr",pd.get("addr"));
            if(pd.get("email")!=null) suppliers.set("email",pd.get("email"));
            return suppliers.update()? R.ok():R.error("修改失败");
        }
        return R.error("请求参数不全");
    }

    /**
     * 删除供应商
     * @param ids
     * @return
     */
    public R remove(List<String> ids){
        GoodsSuppliers suppliers = new GoodsSuppliers();
        int temp=0;
        for (String id : ids) {
            if(id!=null &&!id.equals("")){
                temp+=Db.delete(Db.getSql("jxc.GoodsSuppliers.deleteSuppliersById"),id);
            }
        }
        return temp > 0 ? R.ok() : R.error("删除失败");
    }


}
